package com.kyomonn.talkischeap.DeepSeek;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kyomonn.talkischeap.R;

import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final List<Message> messageList;

    public ChatAdapter(List<Message> messageList) {
        this.messageList = messageList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            // 用户消息布局
            return new UserMessageViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user_message, parent, false));
        } else {
            // AI 消息布局
            return new AiMessageViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ai_message, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Message message = messageList.get(position);

        if (holder instanceof UserMessageViewHolder) {
            ((UserMessageViewHolder) holder).tvMessage.setText(message.getContent());
        } else if (holder instanceof AiMessageViewHolder) {
            ((AiMessageViewHolder) holder).tvMessage.setText(message.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }

    @Override
    public int getItemViewType(int position) {
        Message message = messageList.get(position);
        return message.isUser() ? 0 : 1;
    }

    public void addMessage(Message message) {
        messageList.add(message);
        notifyItemInserted(messageList.size() - 1);
    }

    /**
     * 用户消息 ViewHolder
     */
    public static class UserMessageViewHolder extends RecyclerView.ViewHolder {
        TextView tvMessage;

        public UserMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            tvMessage = itemView.findViewById(R.id.tv_sender_user);
        }
    }

    /**
     * AI 消息 ViewHolder
     */
    public static class AiMessageViewHolder extends RecyclerView.ViewHolder {
        TextView tvMessage;

        public AiMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            tvMessage = itemView.findViewById(R.id.tv_sender_ai);
        }
    }
}

